# -*- coding: utf-8 -*-
############################################################################
from chee.shortcuts import *
from chee.models import *
from chee.models.user import *
############################################################################
class Admin_Upload_File(AdminHandler):
    rule = Rule("/admin/file/upload/")
    def get(self):
        tmpl = env.get_template('admin/game/upload_file.html')
        return Response(tmpl.render(
            request = self.request, errors = self.errors
        ))
    def post(self):
        import os
        self.errors['up_file'] = u''
        up_file = self.request.files.get('up_file')
        if not up_file:
            self.errors['up_file'] = u'没有文件'
            return self.get()
        up_pos = self.request.form.get('up_pos','').strip()
        if not up_pos: up_pos = '/static/data/temp/'
        
        _folder = pathHelper.getFolderAbs(up_pos.split('/'))
        if not os.path.exists(_folder): os.makedirs(_folder) 
        f = open(os.path.join(_folder, up_file.filename), 'wb')
        while True:
            s = up_file.read(10 ** 6)
            if s: f.write(s)
            else: f.close(); break
        self.errors['up_file'] = u'上传成功'
        tmpl = env.get_template('admin/game/upload_file.html')
        return Response(tmpl.render(    
            request = self.request, errors = self.errors
        ))
